//
//  ProductTextCell.m
//
//
//  Created by Abner on 16/4/28.
//
//

#import "ProductTextCell.h"
#import "AddProduct.h"

@interface ProductTextCell ()
{
    UIView *_topLine; //cell最上面的线，UI要求的，只有indexPath.row == 0时显示；
}
@end

@implementation ProductTextCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _topLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        _topLine.backgroundColor = SetColorRGBValue(0xe0e0e0, 1.0);
        _topLine.alpha = 0.5;
        [self.contentView addSubview:_topLine];
        
        self.leftLb = [[UILabel alloc]init];
        [self.contentView addSubview:self.leftLb];
        self.leftLb.font = KAddLeftLbFont;
        self.leftLb.textColor = KAddLeftLbColor;
        self.leftLb.frame = CGRectMake(10, 0, 80, 44);
        
        
        self.detailTf = [[UITextField alloc]init];
        [self.contentView addSubview:self.detailTf];
        self.detailTf.textColor = KAddLeftLbColor;
        self.detailTf.font = KAddLeftLbFont;
        self.detailTf.frame = CGRectMake(90, 0, ScreenWidth-100, 44);
        self.detailTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        [self.detailTf addTarget:self action:@selector(detailTfDidChanged) forControlEvents:UIControlEventEditingChanged];
        //限制输入文字数量pp
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
                                                    name:@"UITextFieldTextDidChangeNotification"
                                                  object:self.detailTf];
        
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 44, ScreenWidth, 1)];
        line.backgroundColor = SetColorRGBValue(0xe0e0e0, 1.0);
        line.alpha = 0.5;
        [self.contentView addSubview:line];
    }
    return self;
}

-(void)setupProductTextCell:(NSString *)leftStr placeholderStr:(NSString *)placeholderStr aLCProduct:(AddProduct *)aLCProduct andRow:(NSInteger)row
{
    self.leftLb.text = leftStr;
    self.detailTf.placeholder = placeholderStr;
    if (aLCProduct.productname.length > 0 && [leftStr isEqualToString:@"商品名称"]) {
        self.detailTf.text = aLCProduct.productname;
    }else if (aLCProduct.sort.length > 0 && [leftStr isEqualToString:@"商品排序"]){
        self.detailTf.text = aLCProduct.sort;
    }else if (aLCProduct.property.length > 0 && [leftStr isEqualToString:@"规格命名"]){
        self.detailTf.text = aLCProduct.property;
    }
    
    if (row == 0) {
        _topLine.hidden = NO;
    }else{
        _topLine.hidden = YES;
    }
    
}
-(void)detailTfDidChanged
{
    if (self.detailTfBlock) {
        self.detailTfBlock(self);
    }
    
}

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *indetifier = @"ProductTextCellIdentifier";
    ProductTextCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
    if (!cell) {
        cell = [[ProductTextCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
    }
    return cell;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
//pp
-(void)textFiledEditChanged:(NSNotification*)obj{
    UITextField *textField = (UITextField *)obj.object;
    if (textField.tag == 6057 || textField.tag == 6058) { //只有商品规格有此限制
        NSInteger limitCount;
        if (textField.tag == 6057) {
            limitCount = 25;
        }else{
            limitCount = 15;
        }
        NSString *toBeString = textField.text;
        // 键盘输入模式
        NSArray *langs = [UITextInputMode activeInputModes];
        UITextInputMode *inputMode = (UITextInputMode *)[langs firstObject];
        NSString *lang = [inputMode primaryLanguage];
        //FIXME: 输出输入的是汉字还是字母
        // NSLog(@"输出输入的是汉字还是字母--%@",lang);
        if([lang isEqualToString:@"zh-Hans"]) { //简体中文输入，包括简体拼音，简体五笔，简体手写
            UITextRange *selectedRange = [textField markedTextRange];
            //获取高亮部分
            UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
            //没有高亮选择的字，则对已输入的文字进行字数统计和限制
     
            if(!position) {
                if(toBeString.length > limitCount) {
                    textField.text = [toBeString substringToIndex:limitCount];
                    [[MessShowView share]showWithMess:[NSString stringWithFormat:@"必须限制在%ld字以内",(long)limitCount] login:NO];

                }
            }
            //有高亮选择的字符串，则暂不对文字进行统计和限制
            else{
                
            }
        }
        //中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        else{
            if(toBeString.length > limitCount) {
                textField.text = [toBeString substringToIndex:limitCount];
                [[MessShowView share]showWithMess:[NSString stringWithFormat:@"必须限制在%ld字以内",(long)limitCount] login:NO];
                
            }
        }

    }
   
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:@"UITextFieldTextDidChangeNotification"
                                                 object:self.detailTf];
}
//pp
@end
